﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>分组平铺</title>
     <script language="JavaScript" src="../../Comm/JScript.js" type="text/javascript" ></script>
    <script type="text/javascript" src="../../Scripts/bootstrap/js/jquery.min.js"></script>
    <script src="../../Scripts/QueryString.js" type="text/javascript"></script>
    <!-- 引用通用的js文件. -->
    <script type="text/javascript" src="../../Scripts/config.js"></script>
    <script type="text/javascript" src="../../Comm/Gener.js"></script>
    <link href="../../../DataUser/Style/ccbpm.css" rel="Stylesheet" />


    <script type="text/javascript">

        var selectedEles = null;
        var mapExt = null;
        var webUser = new WebUser();

        function loadEntities(mapExt) {
            selectedEles = new Entities("BP.Sys.FrmEleDBs");
            selectedEles.Retrieve("FK_MapData", mapExt.FK_MapData, "EleID", mapExt.AttrOfOper, "RefPKVal", GetQueryString("PKVal"));
        }

        function DealSQL(dbSrc) {
            //dbSrc = dbSrc.replace(/~/g, "'");
            if (webUser == null || webUser == undefined)
                webUser = new WebUser();

            //替换表达式常用的用户信息
            dbSrc = dbSrc.replace('@WebUser.No', webUser.No);
            dbSrc = dbSrc.replace('@WebUser.Name', webUser.Name);
            dbSrc = dbSrc.replace("@WebUser.FK_DeptNameOfFull", webUser.FK_DeptNameOfFull);
            dbSrc = dbSrc.replace('@WebUser.FK_DeptName', webUser.FK_DeptName);
            dbSrc = dbSrc.replace('@WebUser.FK_Dept', webUser.FK_Dept);
            
            
            //SQL语句中含有@符号，替换父窗体表单字段值
            if (dbSrc.indexOf("@") != -1) {
                var reg = new RegExp("(\~(.+?)\~)");
                var results = dbSrc.match(reg);
                $.each(results, function (i, param) {
                    param = param.replace("@", "");
                    //获取父窗体中的字段
                    var obj = $('#TB_' + param, parent.document);
                    if (obj.length == 0)
                        obj = $('#DDL_' + param, parent.document);
                    if (obj.length == 0)
                        obj = $('#CB_' + param, parent.document);
                    if (obj.length != 0)
                        dbSrc = dbSrc.replace("@" + param, obj.val());
                });
            }
            dbSrc = dbSrc.replace(/~/g, "'");
            return dbSrc;

        }
        $(function () {

            $("#Msg").html("正在加载,请稍后......");

            var mypk = GetQueryString("FK_MapExt");
            mapExt = new Entity("BP.Sys.MapExt", mypk);

            //获取设置显示的列数
            var showSpan = mapExt.GetPara("ShowCol");
            if (showSpan == null || showSpan == undefined || showSpan == "")
                showSpan = 3;

            var srcOfGroup = mapExt.Tag1; //分组.
            var srcOfEntitis = mapExt.Tag2; //实体.
            srcOfGroup = DealSQL(srcOfGroup);
            srcOfEntitis = DealSQL(srcOfEntitis);
            var groups;
            var entities;
            if (mapExt.DBType == 0) {
                groups = mapExt.DoMethodReturnString("GetDataTableByField", "Tag1","",null,GetQueryString("OID"),null); //分组.
                if (groups.indexOf("err@") != -1) {
                    alert(groups);
                    return;
                }
                groups = JSON.parse(groups);
                entities = mapExt.DoMethodReturnString("GetDataTableByField", "Tag2", "", null, GetQueryString("OID"),null);//数据源.
                if (entities.indexOf("err@") != -1) {
                    alert(entities);
                    return;
                }
                entities = JSON.parse(entities);
            } else {
                groups = DBAccess.RunDBSrc(srcOfGroup, mapExt.DBType); //分组.
                entities = DBAccess.RunDBSrc(srcOfEntitis, mapExt.DBType); //数据源.
            }
            
            

            var html = "<table style='width:98%;' >";

            var isRB = true;
            if (mapExt.GetPara('SelectType') == 1)
                isRB = false;

            //获得已经选择的集合.
            loadEntities(mapExt);

            for (var i = 0; i < groups.length; i++) {
                var group = groups[i];

                html += "<tr>";
                html += "<th colspan=3>";

                if (isRB == true)
                    html += "" + group.Name;
                else
                    html += "<input type='checkbox'  onclick='CheckAll(this.checked, \"" + group.No + "\");' id='Group_" + group.No + "'  /><label for='Group_" + group.No + "'>" + group.Name + "</label>";

                html += "</th>";
                html += "</tr>";


                var myNum = -1;
                for (var idx = 0; idx < entities.length; idx++) {

                    en = entities[idx];

                    var myidx = 0;
                    var oOfEn;
                    for (var obj in en) {
                        if (myidx == 2) {
                            oOfEn = obj;
                            break;
                        }
                        myidx++;
                    }

                    myidx = 0;
                    var oOfGroup;
                    for (var obj in en) {
                        if (myidx == 0) {
                            oOfGroup = obj;
                            break;
                        }
                        myidx++;
                    }

                    if (en[oOfEn] != group[oOfGroup])
                        continue;

                    var isHave = false;
                    for (var iEle = 0; iEle < selectedEles.length; iEle++) {

                        var ele = selectedEles[iEle];
                        if (ele == null || ele == undefined)
                            continue;

                        if (ele.Tag1 == en.No) {
                            isHave = true;
                            break;
                        }
                    }

                    var lab = en.Name;
                    var checked = "";
                    if (isHave) {
                        lab = "<font color=green>" + en.Name + "</font>";
                        checked = "checked";
                    }

                    myNum++;
                    if (myNum == 0)
                        html += "<TR>";

                    if (isRB == true)
                        html += "<td><input type='radio' name='RB' id='RB_" + en.No + "' value='" + en.No + "' " + checked + " /><label for='RB_" + en.No + "'>" + lab + "</label></td>";
                    else
                        html += "<td><input type='checkbox' name='" + group.No + "' id='CB_" + en.No + "' value='" + en.No + "' " + checked + " /><label for='CB_" + en.No + "'>" + lab + "</label></td>";

                    if (myNum == showSpan - 1) {

                        html += "</tr>";
                        myNum = -1;
                    }
                }

                if (myNum == 0) {
                    html += "<td colspan=2></td>";
                    html += "</tr>";
                }

                if (myNum == 1) {
                    html += "<td></td>";
                    html += "</tr>";
                }

            }
            html += "</table>";

            $("#Msg").html("");
            $("#Msg").html(html);
        });

        //选择与取消选择.
        function CheckAll(checked, no) {

            $("input[name='" + no + "']").each(function () {
                this.checked = checked;
            });

        }

        //保存.
        function Save() {

            //获得存储的值.
            var selectVals = [];

            //获得存储的text
            var selectValTexts = [];

            //获得选择的数据.
            var selectType = mapExt.GetPara("SelectType");
            if (selectType == "0") {  //单选.
                selectVals.push($("input[name='RB']:checked").val());
                selectValTexts.push({ No:$("input[name='RB']:checked").val(),Name: $("input[name='RB']:checked").siblings().text() });
            } else {

                var cbs = $("input[type='checkbox']:checked");

                cbs.each(function () {
                    if (this.id.indexOf('CB_') == -1)
                        return true;
                    selectVals.push(this.value);

                    selectValTexts.push({ No: this.value, Name: $(this).siblings().text() });
                });
            }

            //删除变量中没有的数据,已经取消掉的.  selectedEles 数据.
            var FK_MapData = mapExt.FK_MapData;
            var keyOfEn = mapExt.AttrOfOper;
            var oid = GetQueryString("PKVal");
            //
            var insertArray = selectedElements(selectedEles, selectValTexts);
            $.each(insertArray, function (i, o) {
                var frmEleDB = new Entity("BP.Sys.FrmEleDB");
                frmEleDB.MyPK = keyOfEn + "_" + oid + "_" + o.No;
                frmEleDB.FK_MapData = FK_MapData;
                frmEleDB.EleID = keyOfEn;
                frmEleDB.RefPKVal = oid;
                frmEleDB.Tag1 = o.No; 
                frmEleDB.Tag2 = o.Name;
                if (frmEleDB.Update() == 0) {
                    frmEleDB.Insert();
                }
            });

            var deleteArray = canceledElements(selectedEles, selectVals);
            $.each(deleteArray, function (i, o) {
                var frmEleDB = new Entity("BP.Sys.FrmEleDB");
                frmEleDB.MyPK = keyOfEn + "_" + oid + "_" + o;
                frmEleDB.Delete();
            });

            return selectValTexts;
        }

        // 新增
        function selectedElements(entities, selectedArray) {
            var array = [];
            $.each(selectedArray, function (i, e) {
                if (typeof e === "undefined") {
                    return true;
                }
                var exists = false;
                $.each(entities, function (n, o) {
                    if (o.Tag1 == e.No) {
                        exists = true;
                        return false;
                    }
                });
                if (!exists) {
                    array.push(e);
                }
            });
            return array;
        }
        // 删除
        function canceledElements(entities, selectedArray) {
            if (!$.isArray(selectedArray)) {
                return;
            }
            var array = [];
            $.each(entities, function (i, o) {
                var value = o.Tag1;
                var t = selectedArray.filter(function (e) {
                    return e == value;
                });
                if (t.length == 0) {
                    array.push(value);
                }
            });
            return array;
        }
    </script>
</head>
<body>
 
 <table id="Table1" >
  
 </table>

 <input type=button id="Btn_Save" onclick="Save();" style="display: none;" value="Save"/>
 <div id="Msg"> </div>

</body>
</html>
